#include <iostream>
#include <cstring>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <pthread.h>
#include <cassert>
#include <memory>  // 添加unique_ptr的头文件

struct pthread_data {
    struct sockaddr_in client_addr;
    int sock_fd;
};

using namespace std;

void *serverForClient(void *arg);

int main() {
    int socket_fd;
    int conn_fd;
    int res;
    socklen_t len;  // 修正为socklen_t类型

    struct sockaddr_in sever_add;
    memset(&sever_add, 0, sizeof(sever_add));
    sever_add.sin_family = AF_INET;  // 建议使用AF_INET而非PF_INET
    sever_add.sin_addr.s_addr = htonl(INADDR_ANY);  // 修正为htonl
    sever_add.sin_port = htons(8081);
    len = sizeof(sever_add);

    int option = 1;
    socket_fd = socket(AF_INET, SOCK_STREAM, 0);
    assert(socket_fd >= 0);
    setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));

    res = bind(socket_fd, (struct sockaddr*)&sever_add, len);
    perror("bind");
    assert(res != -1);

    res = listen(socket_fd, 5);  // 增加backlog数量
    assert(res != -1);
    cout << "server init" << endl;

    while(1) {
        struct sockaddr_in client;
        socklen_t client_len = sizeof(client);
        conn_fd = accept(socket_fd, (struct sockaddr*)&client, &client_len);
        if (conn_fd < 0) {  // 添加错误检查
            perror("accept");
            continue;
        }
        
        pthread_data* pdata = new pthread_data;
        pdata->client_addr = client;
        pdata->sock_fd = conn_fd;
        
        pthread_t pt;
        cout << "New connection: " << conn_fd << endl;
        if (pthread_create(&pt, NULL, serverForClient, (void*)pdata) != 0) {
            perror("pthread_create");
            delete pdata;
            close(conn_fd);
        }
        pthread_detach(pt);
    }

    close(socket_fd);  // 添加关闭socket
    return 0;
}

void *serverForClient(void *arg) {
    unique_ptr<pthread_data> pdata(static_cast<pthread_data*>(arg));
    int conn_fd = pdata->sock_fd;
    cout << "Processing connection: " << conn_fd << endl;

    char request[1024] = {0};
    int len = recv(conn_fd, request, sizeof(request)-1, 0);
    if (len <= 0) {
        if (len < 0) perror("recv");
        close(conn_fd);
        return nullptr;
    }

    // 完整的HTTP响应
    const char* response = 
        "HTTP/1.1 200 OK\r\n"
        "Connection: close\r\n"
        "Content-Type: text/html\r\n\r\n"
        "<!DOCTYPE html>\n"
        "<html>\n"
        "<head>\n"
        "<title>Welcome to C++ web server!</title>\n"
        "<style>\n"
        "html {color-scheme: light dark;}\n"
        "body {width: 35em; margin: 0 auto;\n"
        "font-family: Tahoma, Verdana, Arial, sans-serif; }\n"
        "</style>\n"
        "</head>\n"
        "<body>\n"
        "<h1>Welcome to C++ webserver</h1>\n"
        "<p>If you see this page, the web server is successfully installed and working.</p>\n"
        "<p><em>Thank you for using webserver.</em></p>\n"
        "</body>\n"
        "</html>\n";

    int s = send(conn_fd, response, strlen(response), 0);
    if (s <= 0) {
        perror("send");
    }
    
    close(conn_fd);
    return nullptr;
}
